package com.task.poker.card.services;


import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

/**
 * Poker Player class.
 * @author	JiDong
 * @create	November 14.2019
 */
public class PokerPlayer implements Runnable {

    private CountDownLatch countDownLatch;
    private PockerDeck pockerDeck;
    private Integer player1Score = 0;
    private Integer player2Score = 0;
    private Integer player3Score = 0;


    public PokerPlayer(PockerDeck pockerDeck, CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
        this.pockerDeck = pockerDeck;
    }

    @Override
    public void run() {
        // locker
        Lock locker = pockerDeck.getLocker();
        // sender
        Condition sender = pockerDeck.getSender();
        // receiver
        Condition receiver = pockerDeck.getReceiver();

        locker.lock();
        try {
            while (!PockerDeck.stop) {
                Map<String, String> mapCard = pockerDeck.getPlayerMap();
                if (mapCard.size() != 3) {
                    try {
                        sender.signal();
                        receiver.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                if (mapCard.size() == 3) {
                    //receiver
                    String player1 = mapCard.get("player1");
                    String player2 = mapCard.get("player2");
                    String player3 = mapCard.get("player3");
                    mapCard.clear();
                    System.out.printf("Round"+PokerSender.getNumber()+"  receiver player1:%s player2:%s player3:%s -->", player1, player2,player3);
                    //sum
                    if (printWin(player1,player2,player3)) {
                        PockerDeck.stop = true;
                        sender.signal();
                        receiver.signalAll();
                        break;
                    }
                }
            }

            System.out.println(Thread.currentThread().getName()+ " countDown player stop:"+ PockerDeck.stop);
            countDownLatch.countDown();
            receiver.signalAll();
        } catch (Exception e) {

        } finally {
            locker.unlock();
        }
    }

    public  boolean printWin(String player1, String player2,String player3) {
        boolean isWin = false;

        String[] cardArray1 = player1.split("=");
        String[] cardArray2 = player2.split("=");
        String[] cardArray3 = player3.split("=");
        synchronized (player1Score){
            player1Score += pockerDeck.getFigure(cardArray1[1]);
        }
        synchronized (player2Score){
            player2Score += pockerDeck.getFigure(cardArray2[1]);
        }
        synchronized (player3Score){
            player3Score += pockerDeck.getFigure(cardArray3[1]);
        }

        String p1w = "";
        String p2w = "";
        String p3w = "";
        if (player1Score >= 50) {
            p1w = "Win";
            isWin = true;
        }
        if (player2Score >= 50) {
            p2w = "Win";
            isWin = true;
        }

        if (player3Score >= 50) {
            p3w = "Win";
            isWin = true;
        }
        System.out.printf("\t player1=%s " + p1w + " player2=%s " + p2w + " player3=%s " + p3w,
                player1Score, player2Score, player3Score);
        System.out.println();
        return isWin;
    }
}
